package org.zfes.snowier.cloud.zbss.cms.content.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.content.service.ICmsContentGroupViewService;
import org.zfes.snowier.cloud.zbss.cms.content.dao.CmsContentGroupViewMapper;
import org.zfes.snowier.cloud.zbss.cms.content.model.CmsContentGroupView;
import java.util.stream.Stream;
@Service
public class CmsContentGroupViewServiceImpl implements ICmsContentGroupViewService{
 	@Autowired
	private CmsContentGroupViewMapper cmsContentGroupViewMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsContentGroupView(CmsContentGroupView cmsContentGroupView) {
		ZBeanUtil.validateBean(cmsContentGroupView);
		cmsContentGroupView.setStatus(Byte.valueOf("1"));//状态:_D#1 启用 0禁用 -1删除#
		cmsContentGroupView.setCreateTime(ZDateUtil.newDate());
		cmsContentGroupView.setUpdateTime(ZDateUtil.newDate());
		cmsContentGroupViewMapper.insertSelective(cmsContentGroupView);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsContentGroupView> loadCmsContentGroupViewById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsContentGroupViewMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsContentGroupView(CmsContentGroupView cmsContentGroupView) {
		ZBeanUtil.validateBean(cmsContentGroupView);
		cmsContentGroupView.setUpdateTime(ZDateUtil.newDate());
		cmsContentGroupViewMapper.updateByPrimaryKeySelective(cmsContentGroupView);
	}
	
	
	@Override
	public void deleteCmsContentGroupView(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsContentGroupViewMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentGroupViewStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentGroupViewMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsContentGroupViewDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsContentGroupViewMapper.selectListCount(pm), cmsContentGroupViewMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsContentGroupView> loadCmsContentGroupViewByContentId(Long contentId) {
		if(contentId!=null){
			return cmsContentGroupViewMapper.selectByContentId(contentId);
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void createCmsContentGroupViewBatch(List<CmsContentGroupView> cmsContentGroupViewList) {
		ZAssert.isNull(cmsContentGroupViewList, "参数错误");
		for(int i=0;i<cmsContentGroupViewList.size();i++) {
			cmsContentGroupViewMapper.insert(cmsContentGroupViewList.get(i));
		}
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsContentGroupViewByContentId(Long contentId) {
		ZAssert.notNull(contentId, "请选择数据");
		cmsContentGroupViewMapper.deleteByContentId(contentId);
	}
	
}